﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;

namespace TCC.CMFlow.Models
{
    public class PerfilAcesso
    {
        public int ID { get; set; }

        [Required]
        public string Nome { get; set; }

        public string Descricao  { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Início vigência")]
        public DateTime? InicioVigencia { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Fim vigência")]
        public DateTime? FimVigencia { get; set; }

        [Display(Name = "Status")]
        public string Status { get { return this.IsAtivo ? "Ativo" : "Inativo"; } }

        public virtual ICollection<Usuario> Usuarios { get; set; }

        public virtual bool IsAtivo
        {
            get
            {
                return IsAtivoExpression.Compile()(this);
            }
        }

        public static Expression<Func<PerfilAcesso, bool>> IsAtivoExpression
        {
            get
            {
                return t => t.InicioVigencia.HasValue && t.InicioVigencia.Value <= DateTime.Today && (!t.FimVigencia.HasValue || t.FimVigencia.Value >= DateTime.Today);
            }
        }

        public static Expression<Func<PerfilAcesso, bool>> IsInativoExpression
        {
            get
            {
                return t => !(t.InicioVigencia.HasValue && t.InicioVigencia.Value <= DateTime.Today && (!t.FimVigencia.HasValue || t.FimVigencia.Value >= DateTime.Today));
            }
        }
    }
}